package com.zxj.system.controller.sysuser;


import com.zxj.common.core.contains.HttpConstants;
import com.zxj.common.core.controller.BaseController;
import com.zxj.common.core.domain.R;
import com.zxj.system.domain.sysuser.dto.LoginDTO;
import com.zxj.system.domain.sysuser.dto.SysUserAddDTO;
import com.zxj.system.domain.sysuser.vo.LoginUserVO;
import com.zxj.system.domain.sysuser.vo.LoginVO;
import com.zxj.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统管理员用户表 前端控制器
 * </p>
 *
 * @author zxj2022
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/sys-user")
@Tag(name = "管理员接口" , description = "管理员相关接口")
@Slf4j
@RequiredArgsConstructor
public class SysUserController extends BaseController {
    private final ISysUserService sysUserService;
    /**
     * 登录
     * @param loginDTO 登录参数
     * @return 登录返回对象
     */
    @PostMapping("/login")
    @Operation(summary = "管理员登录", description = "根据账号密码进行管理员登录")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    @ApiResponse(responseCode = "3103", description = "用户名或密码错误")
    public R<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("接收到管理员登录请求: {}", loginDTO);
        return R.ok(sysUserService.login(loginDTO));
    }


    /**
     * 新增管理员, 开发测试调用接口
     * @param sysUserAddDTO 新增管理员参数
     * @return 操作结果
     */
    @PostMapping("/add")
    @Operation(summary = "新增管理员", description = "根据提供的信息新增管理员")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户已存在")
    public R<Void> add(@RequestBody SysUserAddDTO sysUserAddDTO) {
        log.info("接收到新增管理员请求: {}", sysUserAddDTO);
        return this.toR(sysUserService.add(sysUserAddDTO));
    }



    /**
     * 接口地址：/system/sysUser/info
     * @param token 请求头token信息
     * @return 获取登录用户信息
     */
    @GetMapping("/info")
    @Operation(summary = "获取用户信息", description = "从jwt token中获取用户信息")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "请求头token信息", required = true,in = ParameterIn.HEADER)
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3001", description = "未授权")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("接收到获取用户信息请求: token={}", token);
        return sysUserService.info(token);
    }

    /**
     * 退出登录, 接口地址：/system/sysUser/logout
     * @param token
     * @return
     */
    @DeleteMapping("/logout")
    @Operation(summary = "退出登录", description = "退出登录, 依据token删除redis缓存")
    @Parameter(name = HttpConstants.AUTHENTICATION, description = "请求头token信息", required = true,in = ParameterIn.HEADER)
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3001", description = "未授权")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("接收到退出登录请求: token={}", token);
        return toR(sysUserService.logout(token));
    }
}